home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia 1995 April / Informatica Multimedia CD - Epimundo.iso / DOS / FILEFIND / FFF / MATCH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-21  |  1.8 KB  |  94 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4. static int I_Match (char *Str, char *Pat);
  5. static int S_Match (char *S, char *P, int Anchor);
  6.  
  7.  
  8.  int
  9. Match (char *Str, char *Pat) {
  10.     char S_Name[66], S_Ext[4];
  11.     char P_Name[66], P_Ext[4];
  12.     char *p1;
  13.  
  14.     if ( (p1 = strrchr(Str, '.')) != NULL ) {
  15.         *p1 = '\0';
  16.         strcpy(S_Name, Str);
  17.         strcpy(S_Ext, p1+1);
  18.         *p1 = '.';
  19.         }
  20.     else {
  21.         strcpy(S_Name, Str);
  22.         S_Ext[0] = '\0';
  23.         }
  24.  
  25.     if ( (p1 = strchr(Pat, '.')) != NULL ) {
  26.         *p1 = '\0';
  27.         strcpy(P_Name, Pat);
  28.         strcpy(P_Ext, p1+1);
  29.         *p1 = '.';
  30.         }
  31.     else {
  32.         strcpy(P_Name, Pat);
  33.         strcpy(P_Ext, "*");
  34.         }
  35.  
  36.     if ( !I_Match(S_Name, P_Name) ) return(0);
  37.     if ( (P_Ext[0] == '\0') && (S_Ext[0] != '\0') ) return(0);
  38.     if ( !I_Match(S_Ext, P_Ext) ) return(0);
  39.     return(1);
  40.     }
  41.  
  42.  
  43.  static int
  44. I_Match (char *Str, char *Pat) {
  45.     char *p, *p1, *p2, Hold;
  46.     int t;
  47.  
  48.     if ( (p1 = strchr(Pat, '*')) == NULL)
  49.         return( S_Match(Str, Pat, 1) );
  50.     if (Pat[0] != '*') {
  51.         *p1 = '\0';
  52.         t = S_Match(Str, Pat, 0);
  53.         *p1 = '*';
  54.         if (!t) return(0);
  55.         }
  56.     if (Pat[strlen(Pat)-1] != '*') {
  57.         p2 = strrchr(Pat, '*') + 1;
  58.         if (strlen(Str) < strlen(p2)) return(0);
  59.         if ( !S_Match(&Str[strlen(Str) - strlen(p2)], p2, 1) )
  60.             return(0);
  61.         }
  62.  
  63.     p = Str;
  64.     while ( (p2 = strchr(++p1, '*')) != NULL ) {
  65.         *p2 = '\0';
  66.         Hold = p1[0];
  67.         while ( (p = strchr(p, Hold)) != NULL ) {
  68.             if ( S_Match(p, p1, 0) ) break;
  69.             ++p;
  70.             }
  71.         if (p == NULL) return(0);
  72.         p += strlen(p1);
  73.         *p2 = '*';
  74.         p1 = p2;
  75.         }
  76.     return(1);
  77.     }
  78.  
  79.  
  80.  static int
  81. S_Match (char *S, char *P, int Anchor) {
  82.  
  83.     while ( (*P != '\0') && (*S != '\0') ) {
  84.         if ( (*S == *P) || (*P == '?') ) {
  85.             S++;
  86.             P++;
  87.             }
  88.         else return(0);
  89.         }
  90.     if (*P != '\0') return(0);
  91.     if ( Anchor && (*S != '\0') ) return(0);
  92.     return(1);
  93.     }
  94.